﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LeSoft.Application.Bussiness;
using LeSoft.Application.Cache;
using LeSoft.Application.Code;
using LeSoft.Application.Entity;
using LeSoft.Util;
using System.Text;
using System.Data;
using System.Collections;
using Newtonsoft.Json;

namespace LeSoft.Application.Web.Areas.QualificationManage.Controllers
{
    public class RentContractController : MvcControllerBase
    {

        /// <summary>
        /// 创建人：yzp
        /// 日 期：2017.09.15
        /// 描 述：房屋租赁合同明细
        /// </summary>
        //
        // GET: /QualificationManage/RentContract/Index

        private static ContractDWellingBLL cdwbll = new ContractDWellingBLL();
        private OrganizeCache organizeCache = new OrganizeCache();
        private RoomBLL rbll = new RoomBLL();
        private PStructBLL pstructBLL = new PStructBLL();
        #region 视图
        [HttpGet]
        [HandlerAuthorize(PermissionMode.Enforce)]
        [HandlerAction(OperationType.Visit, "合同首页", "Index")]
        public ActionResult Index()
        {
            return View();
        }
        [HttpGet]
        [HandlerAuthorize(PermissionMode.Ignore)]
        [HandlerAction(OperationType.Visit, "合同详情", "Form")]
        public ActionResult Form()
        {
            return View();
        }

        #endregion

        #region 获取数据
        [HttpGet]
        [HandlerAuthorize(PermissionMode.Ignore)]
        [HandlerAction(OperationType.GetList, "数据列表", "GetPagList")]
        public ActionResult GetPageList(Pagination pagination, string queryJson)
        {

            var data = cdwbll.GetPagList(pagination, queryJson);
            var watch = CommonHelper.TimerStart();
            var JsonData = new
            {
                rows = data,
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                costtime = CommonHelper.TimerEnd(watch)
            };
            return Content(JsonData.ToJson());
        }
        [HttpGet]
        [HandlerAuthorize(PermissionMode.Ignore)]
        [HandlerAction(OperationType.GetList, "合同详情", "GetInfo")]
        public ActionResult GetInfo(string keyValue)
        {
            var data = cdwbll.GetInfo(keyValue);
            return Content(data);
        }
        [HttpGet]
        [HandlerAuthorize(PermissionMode.Ignore)]
        [HandlerAction(OperationType.GetList, "合同状态树", "GetTreeListTojson")]
        public ActionResult GetTreeListTojson(string OrganizeId)
        {

            List<TreeStatusEntity> lst = new List<TreeStatusEntity>();
            lst.Add(new TreeStatusEntity("0", EnumAttribute.GetDescription(ContractEnum.All), ContractEnum.All.ToString(), 0));
            lst.Add(new TreeStatusEntity(ContractEnum.All.ToString(), EnumAttribute.GetDescription(ContractEnum.UnSign), ContractEnum.UnSign.ToString(), 0));
            lst.Add(new TreeStatusEntity(ContractEnum.All.ToString(), EnumAttribute.GetDescription(ContractEnum.UnHandled), ContractEnum.UnHandled.ToString(), 0));
            lst.Add(new TreeStatusEntity(ContractEnum.All.ToString(), EnumAttribute.GetDescription(ContractEnum.Current), ContractEnum.Current.ToString(), 0));
            lst.Add(new TreeStatusEntity(ContractEnum.All.ToString(), EnumAttribute.GetDescription(ContractEnum.History), ContractEnum.History.ToString(), 0));
            lst.Add(new TreeStatusEntity(ContractEnum.All.ToString(), EnumAttribute.GetDescription(ContractEnum.Complete), ContractEnum.Complete.ToString(), 0));
            lst.Add(new TreeStatusEntity(ContractEnum.Current.ToString(), EnumAttribute.GetDescription(ContractEnum.Days60), ContractEnum.Days60.ToString(), 0));
            lst.Add(new TreeStatusEntity(ContractEnum.Current.ToString(), EnumAttribute.GetDescription(ContractEnum.WaitingCancel), ContractEnum.WaitingCancel.ToString(), 0));
            lst.Add(new TreeStatusEntity(ContractEnum.Current.ToString(), EnumAttribute.GetDescription(ContractEnum.Changing), ContractEnum.Changing.ToString(), 0));
            lst.Add(new TreeStatusEntity(ContractEnum.Current.ToString(), EnumAttribute.GetDescription(ContractEnum.Renewing), ContractEnum.Renewing.ToString(), 0));
            lst.Add(new TreeStatusEntity(ContractEnum.Current.ToString(), EnumAttribute.GetDescription(ContractEnum.Leaseing), ContractEnum.Leaseing.ToString(), 0));
            lst.Add(new TreeStatusEntity(ContractEnum.History.ToString(), EnumAttribute.GetDescription(ContractEnum.ExRenew), ContractEnum.ExRenew.ToString(), 0));
            lst.Add(new TreeStatusEntity(ContractEnum.History.ToString(), EnumAttribute.GetDescription(ContractEnum.ExLeaseing), ContractEnum.ExLeaseing.ToString(), 0));
            lst.Add(new TreeStatusEntity(ContractEnum.History.ToString(), EnumAttribute.GetDescription(ContractEnum.BeforeLease), ContractEnum.BeforeLease.ToString(), 0));
            lst.Add(new TreeStatusEntity(ContractEnum.History.ToString(), EnumAttribute.GetDescription(ContractEnum.LaterLease), ContractEnum.LaterLease.ToString(), 0));
            lst.Add(new TreeStatusEntity(ContractEnum.History.ToString(), EnumAttribute.GetDescription(ContractEnum.ExChanging), ContractEnum.ExChanging.ToString(), 0));
            List<TreeEntity> treeList = new List<TreeEntity>();
            foreach (TreeStatusEntity item in lst)
            {
                TreeEntity tree = new TreeEntity();
                bool hasChildren = lst.Count(t => t.PValue == item.Value) == 0 ? false : true;
                tree.id = item.Value;
                tree.text = item.Text;
                tree.value = item.Value;
                if (item.PValue == "0") tree.isexpand = true;
                else tree.isexpand = false;
                tree.complete = true;
                tree.hasChildren = hasChildren;
                tree.parentId = item.PValue;
                treeList.Add(tree);
            }
            return Content(treeList.TreeToJson());
        }

        #endregion

        #region 保存数据
        [HttpPost]
        [ValidateAntiForgeryToken]
        [AjaxOnly]
        [HandlerAction(OperationType.Create, "保存合同", "SaveForm")]
        public ActionResult SaveForm(string keyValue, string type, string postData)
        {
            int Type = Convert.ToInt32(type);
            try
            {
                cdwbll.SaveForm(keyValue, Type, postData);
                return Success("操作成功");
            }
            catch (Exception e)
            {
                if (e.Message == "费用产生失败") return Success("合同签约成功，产生费用失败！");
                //return Success("操作成功");
                return Error("操作失败：" + e.Message);


            }
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        [AjaxOnly]
        [HandlerAction(OperationType.Create, "过渡合同保存", "SaveForm")]
        public ActionResult KeepRent(string keyValue, string type, string queryJson)
        {
            int Type = Convert.ToInt32(type);
            try
            {
                cdwbll.KeepRent(keyValue, Type, queryJson);
                return Success("操作成功");
            }
            catch (Exception e)
            {
                if (e.Message == "费用产生失败") return Success("合同启用成功，产生费用失败！");
                return Success("操作成功");
                // return Error("操作失败：" + e.Message);
            }
        }

        /// <summary>
        /// 批量操作
        /// </summary>
        /// <param name="Type">1-启用 2-取消</param>
        /// <param name="postData">合同单据ID集合</param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        [AjaxOnly]
        [HandlerAction(OperationType.Create, "批量操作", "AllEdit")]
        public ActionResult AllEdit(int Type, string postData)
        {
            try
            {
                cdwbll.AllEdit(Type, postData);
                return Success("操作成功！");
            }
            catch (Exception e)
            {
                if (e.Message == "费用产生失败") return Success("合同启用成功，产生费用失败！");
                return Error("操作失败：" + e.Message);

            }
        }
        /// <summary> 
        /// Create：yzp
        /// Date:2018-01-27
        /// Memo:取消合同
        /// </summary>
        /// <param name="keyvalue"></param>
        [HttpPost]
        [ValidateAntiForgeryToken]
        [AjaxOnly]
        [HandlerAction(OperationType.Create, "取消合同", "CancelContract")]
        public ActionResult CancelContract(string keyValue)
        {
            try
            {
                cdwbll.CancelContract(keyValue);
                return Success("操作成功！");
            }
            catch (Exception e)
            {
                return Error("操作失败：" + e.Message);
            }
        }
        #endregion

        #region 数据导出
        /// <summary>
        /// 资料导出
        /// </summary>
        public ActionResult GetExportList(string queryJson)
        {
            new ExportBLL().GetExportList((int)ExportType.Contract, queryJson);
            return Success("导出成功。");
        }
        #endregion
    }
}
